import { loginByUsername } from '@/api/login'
import { asyncRouterMap } from '@/router/router'
import { getToken, setToken, removeToken, setUser, removeUser, getUser, setUserName, setRouters } from '@/utils/auth'
/**
 * 递归过滤异步路由表，返回符合用户角色权限的路由表
 * @param routes asyncRouterMap
 * @param roles
 */
function filterAsyncRouter (routes, roles) {
    const res = []
    routes.forEach(route => {
        const tmp = { ...route }
        if (tmp.children) {
            tmp.children = filterAsyncRouter(tmp.children, roles)
        }
        res.push(tmp)
    })
    return res
}
const user = {
    state: {
        user: getUser(),
        token: getToken(),
        name: '',
        account: '',
        avatar: '',
        asyncRouters: []
    },

    mutations: {
        SET_ROUTERS: (state, routers) => {
            state.asyncRouters = routers
        },
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_USER: (state, user) => {
            state.setting = user
        },
        SET_NAME: (state, name) => {
            state.name = name
        },
        SET_ACCOUNT: (state, account) => {
            state.account = account
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar
        }
    },

    actions: {
        // 用户名登录
        LoginByUsername ({ commit }, userInfo) {
            const username = userInfo.account.trim()
            return new Promise((resolve, reject) => {
                loginByUsername(username, userInfo.password)
                    .then(response => {
                        const data = response.data
                        commit('SET_TOKEN', data.ticket)
                        commit('SET_NAME', data.adminUser.name)
                        commit('SET_ACCOUNT', data.adminUser.account)
                        commit('SET_USER', data.adminUser)
                        setToken(data.ticket)

                        setUser(data.adminUser)
                        setUserName(data.adminUser.name)
                        resolve(data)
                    })
                    .catch(error => {
                        reject(error)
                    })
            })
        },
        GenerateRoutes ({ commit }, routers) {
            return new Promise(resolve => {
                let accessedRouters = asyncRouterMap[0].children.filter(item => routers.map(r => r.appname).includes(item.meta.title))
                accessedRouters.sort((a, b) => {
                    let ra
                    let rb
                    for (let i of routers) {
                        if (a.meta.title === i.appname) {
                            ra = i
                        }
                    }
                    for (let j of routers) {
                        if (b.meta.title === j.appname) {
                            rb = j
                        }
                    }
                    return ra.ordernum - rb.ordernum
                })
                commit('SET_ROUTERS', accessedRouters)
                setRouters(accessedRouters)
                resolve()
            })
        },
        // 登出
        LogOut ({ commit }) {
            return new Promise((resolve, reject) => {
                commit('SET_TOKEN', '')
                commit('SET_NAME', '')
                commit('SET_ACCOUNT', '')
                commit('SET_USER', '')
                removeToken()
                removeUser()
                resolve()
            })
        }
    }
}

export default user
